package top.lyjwn.ajui.widget.list;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.xuexiang.xui.widget.statelayout.MultipleStatusView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import top.lyjwn.ajui.R;
import top.lyjwn.ajui.widget.list.refresh.ISmartRefreshLayout;
import top.lyjwn.ajui.widget.recycler.RecyclerVerticalView;
import top.lyjwn.ajui.widget.recycler.base.RecyclerBaseListAdapter;

/**
 * 基于recycleView的列表视图
 *
 * @author luoyujie
 */
public class ListView extends LinearLayout {

    private RecyclerVerticalView i_recycler;

    private ISmartRefreshLayout i_refresh;

    private MultipleStatusView i_status_view;

    private Integer themeColorId;

    public ListView(Context context) {
        this(context, null);
    }

    public ListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.listView);
        themeColorId = ta.getResourceId(R.styleable.listView_theme_color, R.color.colorPrimary);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.widget_list_view, this);
        i_recycler = view.findViewById(R.id.i_recycler);
        i_refresh = view.findViewById(R.id.i_refresh);
        i_status_view= view.findViewById(R.id.i_status_view);
        i_status_view.setOnRetryClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
        if (themeColorId != null) {
            i_refresh.setPrimaryColorsId(themeColorId, themeColorId);
        } else {
            i_refresh.setPrimaryColorsId(R.color.colorPrimary, R.color.colorPrimary);
        }
        i_refresh.setPrimaryColorsIdToHeader(R.color.分割线, R.color.默认的文字颜色);
    }

    public void setAdapter(RecyclerBaseListAdapter adapter) {
        i_recycler.setAdapter(adapter);
    }

    public void setOnScrollListener(final OnScrollListener onScrollListener) {
        i_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                onScrollListener.onGround(refreshLayout);
            }
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            i_recycler.setOnScrollChangeListener(new OnScrollChangeListener() {
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    if (onScrollListener != null) {
                        onScrollListener.onScrollY(i_recycler.computeVerticalScrollOffset());
                    }
                }
            });
        } else {
            i_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
//                boolean b = recyclerView.canScrollVertically(1);//的值表示是否能向上滚动，false表示已经滚动到底部
//                boolean b1 = recyclerView.canScrollVertically(-1);//的值表示是否能向下滚动，false表示已经滚动到顶部
                    if (onScrollListener != null) {
//                    if (!b) {
//                        onScrollListener.onGround(i_refresh);
//                    }
                        onScrollListener.onScrollY(i_recycler.computeVerticalScrollOffset());
                    }
                }
            });
        }
    }

    public void setOnRefreshListener(final OnRefreshListener onRefreshListener) {
        i_refresh.setOnRefreshListener(new com.scwang.smartrefresh.layout.listener.OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (onRefreshListener != null) {
                    onRefreshListener.onRefresh(i_refresh);
                }
            }
        });
    }

    public void initData() {
        i_refresh.autoRefresh();
    }

    public void toTop() {
        i_recycler.scrollToPosition(20);
        i_recycler.smoothScrollToPosition(0);
    }

    public void haveData(boolean is){
        if (is ){
            if (i_status_view.getVisibility() != GONE){
                i_status_view.setVisibility(GONE);
            }
        }else{
            i_status_view.setVisibility(VISIBLE);
            i_status_view.showEmpty();

        }
    }


    /**
     * 刷新事件
     */
    public interface OnRefreshListener {
        void onRefresh(SmartRefreshLayout refreshLayout);
    }

    /**
     * 滚动的回调接口
     *
     * @author xiaanming
     */
    public interface OnScrollListener {
        /**
         * 回调方法， 返回MyScrollView滑动的方向
         *
         * @param scrollY
         */
        void onScrollY(int scrollY);

        /**
         * 触底事件
         */
        void onGround(RefreshLayout refreshLayout);

    }


}
